import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { FlowService } from "./flow.service";
import { TableComponent } from "../../modal/table/table.component";
import * as _ from 'lodash';
//updated by dujh @20180627 start
import { Util } from 'app/common/util';
//updated by dujh @20180627 end
@Component({
  selector: 'app-flow',
  templateUrl: './flow.component.html',
  styleUrls: ['./flow.component.scss'],
  providers: [FlowService]
})
export class FlowComponent implements OnInit {
  // 模版
  @ViewChild("flowTable", { read: ViewContainerRef })
  flowTable: ViewContainerRef;
  flowTableComponent: ComponentRef<TableComponent>;
  // 用于识别指标类型
  public type: any;
  // 上方查询体
  public getFlowBody: any = {
    nIxId: '',
    nIjId: '',
    nFlowId: ''
  }
  // 上方流程列表数据
  public flowList: any;
  // 下方查询体
  public getBody: any = {
    beginDate: '',
    endDate: '',
    nIxId: '',
    nIjId: '',
    nFlowId: ''
  }
  // 列表新数据
  public dataList: any = [];
  // 克隆列表新数据,用于页面数据标红对比
  public isChangeList: any = [];
  // 用于是否显示下拉框
  public isEditList: any = [];

  constructor(
    public resolver: ComponentFactoryResolver,
    public flowService: FlowService
  ) { }

  ngOnInit() {
    // 加载table
    this.showFlowTable();
    this.getFlow();
    this.list();
  }
  // 上方流程列表
  getFlow() {
    let that = this;
    this.flowService.getFlow(this.getFlowBody).subscribe(data => {
      if (data) {
        that.flowList = data;
      }
    })
  }
  // 下方多级列表
  list() {
    let that = this;
    // updated by dujh @20180627 start
    // if (this.getBody.beginDate && this.getBody.endDate) {
    //   let begin = +this.getBody.beginDate.split("-").join("");
    //   let end = +this.getBody.endDate.split("-").join("");
    //   if (begin > end) {
    //     window['swal']("提示", "开始日期必须早于结束日期", "warning");
    //     return;
    //   }
    // }
    //改为原来项目中使用的相同检验方法
    if (!Util.dateOrder(this.getBody.beginDate, this.getBody.endDate)){
      window['swal']("提示", "开始日期必须早于结束日期", "warning");
      return ;
    }
    //updated by dujh @20180627 end

    new Promise((resolve, reject) => {
      this.flowService.list(this.getBody).subscribe(data => {
        if (data) {
          that.dataList = data;
          // 用于标红对比
          that.isChangeList = _.cloneDeep(data);
          // 用于是否显示下拉框
          that.isEditList = _.cloneDeep(data);
          _.forEach(that.dataList, (item, index: any) => {
            if (index > 0) {
              // 前后条数据对比
              _.forIn(item, (value, key) => {
                if (that.dataList[index - 1][key] == that.dataList[index][key]) {
                  that.isChangeList[index][key] = false;
                  that.isEditList[index][key] = 'no';
                } else {
                  that.isChangeList[index][key] = true;
                  that.isEditList[index][key] = 'no';
                }
              })
            } else {
              // 第一条数据不作对比 
              _.forIn(item, (value, key) => {
                that.isChangeList[index][key] = false;
                that.isEditList[index][key] = 'no';
              })
            }
          })
          resolve(data);
        }
      })
    }).then(data => {
      that.flowTableComponent.instance.isCheckedAll = false;
      that.flowTableComponent.instance.dataList = _.cloneDeep(that.dataList);
      that.flowTableComponent.instance.isChangeList = _.cloneDeep(that.isChangeList);
      that.flowTableComponent.instance.isEditList = _.cloneDeep(that.isEditList);
    })
  }

  // 关闭流程状态模态框
  closeFolw() {
    window['$']('#historyFlow').modal('hide');
    window['$']('#viewTargetFlow').modal('hide');
    window['$']('#modifyTargetFlow').modal('hide');
    window['$']('#targetReviewFlow').modal('hide');
    window['$']('#taretReviewQueryFlow').modal('hide');
    this.flowTableComponent.destroy();
  }
  // 提醒页面模版
  showFlowTable() {
    if (this.flowTableComponent && this.flowTableComponent.instance) {
      this.flowTableComponent.destroy();
    }
    const flowTableComponent = this.resolver.resolveComponentFactory(TableComponent);
    this.flowTableComponent = this.flowTable.createComponent(flowTableComponent);
    this.flowTableComponent.instance.isShowCol1 = 'sFundCode';
    this.flowTableComponent.instance.isShowCol2 = 'sFullName';
    this.flowTableComponent.instance.isShowCol3 = 'sOprDate';
  }

}
